package com.lazy.controller.repair;

import com.lazy.dto.CategoryDTO;
import com.lazy.dto.CategoryPageQueryDTO;
import com.lazy.entity.repair.Category;
import com.lazy.entity.repair.RepairCategoriesSubcategory;
import com.lazy.result.PageResult;
import com.lazy.result.Result;
import com.lazy.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/category")
@CrossOrigin(origins = "http://localhost:8080")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 分类分页查询
     * @param categoryPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    public Result<PageResult> page(CategoryPageQueryDTO categoryPageQueryDTO){
//        log.info("分页查询:{}",categoryPageQueryDTO);
        PageResult pageResult = categoryService.pageQuery(categoryPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 查询分类
     */
    @GetMapping("/selectCategory")
    // 查询分类
    public Result<List<Category>> selectCategory(){
        List<Category> categories = categoryService.selectCategory();
        return Result.success(categories);
    }

    /**
     * 查询维修子类
     */
    @GetMapping("/selectSubcategory")
    public Result selectSubcategory(){
        List<RepairCategoriesSubcategory> repairCategoriesSubcategories = categoryService.selectSubcategory();
        return Result.success(repairCategoriesSubcategories);
    }

    /**
     * 新增维修分类
     * @param categoryDTO
     * @return
     */
    @PostMapping("/add")
    public Result<String> add(@RequestBody CategoryDTO categoryDTO){
        categoryService.add(categoryDTO);
        log.info("新增分类成功" + categoryDTO);
        return Result.success();
    }

    /**
     * 根据id删除维修分类
     * @param id
     * @return
     */
    @DeleteMapping("/deleteById/{id}")
    public Result<String> delete(@PathVariable List<Integer> id){
        log.info("根据id删除分类:{}",id);
        categoryService.delete(id);
        return Result.success();
    }

    @GetMapping("/selectById/{id}")
    public Result<Category> selectById(@PathVariable Integer id){
        Category category = categoryService.selectById(id);
        return Result.success(category);
    }

    @PutMapping("/update")
    public Result<String> update(@RequestBody CategoryDTO categoryDTO){
//        log.info("修改分类:{}",categoryDTO);
        categoryService.update(categoryDTO);
        return Result.success();
    }

}
